package ai.peanut.selfbase.modules.menu.service.impl;

import ai.peanut.selfbase.common.domain.DataResult;
import ai.peanut.selfbase.common.utils.UserUtils;
import ai.peanut.selfbase.modules.menu.dao.MenuMapper;
import ai.peanut.selfbase.modules.menu.entity.Menu;
import ai.peanut.selfbase.modules.menu.pojo.MenuParams;
import ai.peanut.selfbase.modules.menu.pojo.MenuTreeDto;
import ai.peanut.selfbase.modules.sys.pojo.ZTreeDto;
import ai.peanut.selfbase.modules.menu.service.IMenuService;
import ai.peanut.selfbase.modules.user.pojo.UserDto;
import org.springframework.beans.BeanUtils;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author lichong
 * @since 2019-02-15
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    /**
     * 通过userid查找菜单id集合
     * @param userId
     * @return
     */
    @Override
    public List<Long> getMenuIdListByUserId(Long userId){
        return baseMapper.getMenuIdListByUserId(userId) ;
    }

    /**
     * 获取菜单列表
     * @param params
     * @return
     */
    @Override
    public List<Menu> selectAllByParams(MenuParams params){
        return baseMapper.selectAllByParams( params ) ;
    }



    /**
     * 根据用户id获取菜单列表
     * @return
     */
    @Override
    public List<Menu> selectMenuListByUserId(Long userId){
        MenuParams params = new MenuParams() ;
        if( userId != 1 ) {
            List <Long> menuIdList =this.getMenuIdListByUserId(userId) ;
            if( menuIdList == null || menuIdList.size() == 0 ) {
                return null ;
            }
            params.setMenuIds(menuIdList);
        }
        List<Menu> listMenu = this.selectAllByParams( params ) ;
        return listMenu ;
    }
    /**
     * 首页菜单树列表
     * @return
     */
    @Override
    public List<MenuTreeDto> selectMenuTreeListForIndex(){
        UserDto user = UserUtils.getCurrentUser() ;
        if(user == null ) {
            return null ;
        }
        MenuParams params = new MenuParams() ;
        params.setDelFlag( 1 ) ;
        params.setMenuTypeStr("1,2");
        params.setShowFlag( 1 ) ;
        if( user.getId().intValue() != 1 ) {
            List <Long> menuIdList =this.getMenuIdListByUserId(user.getId()) ;
            if( menuIdList == null || menuIdList.size() == 0 ) {
                return null ;
            }
            params.setMenuIds(menuIdList);
        }
        List<Menu> listMenu = this.selectAllByParams( params ) ;
        return this.generateMenuTreeList(listMenu) ;
    }


    private List<MenuTreeDto> generateMenuTreeList(List<Menu> listMenu){
        if( listMenu == null && listMenu.size() == 0 ) {
            return null ;
        }
        Map< String , MenuTreeDto > mapMenu = new HashMap< String , MenuTreeDto >() ;
        for( int i = 0 ; i< listMenu.size() ; i ++ ) {
            Menu menu = ( Menu )listMenu.get( i ) ;
            MenuTreeDto menuTreeDto = new MenuTreeDto() ;
            BeanUtils.copyProperties( menu , menuTreeDto ) ;
            mapMenu.put( menu.getId().toString() ,  menuTreeDto ) ;
        }

        List<MenuTreeDto> listMenuTreeDto = new ArrayList<>() ;
        Set<Map.Entry< String , MenuTreeDto >> entryset = mapMenu.entrySet() ;

        for( Map.Entry< String , MenuTreeDto > e : entryset) {
            String id = e.getKey() ;
            MenuTreeDto menuTreeDto = e.getValue() ;
            if( menuTreeDto.getParentId().intValue() == 0 ) {
                listMenuTreeDto.add( menuTreeDto ) ;
            } else {
                mapMenu.get( menuTreeDto.getParentId().toString() ).add( menuTreeDto ) ;
            }
        }

        return listMenuTreeDto ;
    }

    /**
     * 根据角色id查找菜单列表  ztree类型
     * @return
     */
    @Override
    public List<ZTreeDto> selectMenuZTreeListByRoleId(Long roleId){
        List<ZTreeDto> ztreeList = new ArrayList<>() ;
        MenuParams params = new MenuParams() ;
        params.setDelFlag( 1 ) ;
        List<Menu> listAllMenu = this.selectAllByParams( params ) ;
        if( listAllMenu == null || listAllMenu.size() == 0 ) {
            return ztreeList ;
        }

        List<Menu> menuRoleList = null ;
        if(roleId != null) {
            params.setRoleId( roleId ) ;
            menuRoleList = baseMapper.selectMenuListByRoleId(params) ;
        }

        for( Menu menu : listAllMenu ) {
            ZTreeDto zTreeDto = new ZTreeDto() ;
            zTreeDto.setId( menu.getId() ) ;
            zTreeDto.setParentId( menu.getParentId() ) ;
            zTreeDto.setName( menu.getName() ) ;
            zTreeDto.setOpen(menu.getMenuType() == 1 ? true:false ) ; //目录
            zTreeDto.setChecked(false);
            if (menuRoleList != null && menuRoleList.size() > 0) {
                for (Menu m : menuRoleList) {
                    if( m.getId().intValue() == menu.getId().intValue() ) {
                        zTreeDto.setChecked(true);
                        break ;
                    }
                }
            }
            ztreeList.add(zTreeDto) ;
        }

        return ztreeList ;
    }


    /**
     * 菜单列表 不包括按钮的所有  ztree类型
     * @return
     */
    @Override
    public List<ZTreeDto> getMenuTreeNotInIconList(Long id){
        Long parentId = 0L ;
        if( id != null ) {
            Menu m = baseMapper.selectById( id ) ;
            parentId = m.getParentId() ;
        }
        List<ZTreeDto> ztreeList = new ArrayList<>() ;
        MenuParams params = new MenuParams() ;
        params.setMenuTypeStr("1,2");
        params.setShowFlag( 1 ) ;
        List<Menu> listAllMenu = this.selectAllByParams( params ) ;
        if( listAllMenu == null || listAllMenu.size() == 0 ) {
            return ztreeList ;
        }
        for( Menu menu : listAllMenu ) {
            ZTreeDto zTreeDto = new ZTreeDto() ;
            zTreeDto.setId( menu.getId() ) ;
            zTreeDto.setParentId( menu.getParentId() ) ;
            zTreeDto.setName( menu.getName() ) ;
            zTreeDto.setOpen(menu.getMenuType() == 1 ? true:false ) ; //目录
            zTreeDto.setChecked(false);
            if ( parentId.intValue() == menu.getId().intValue() ) {
                zTreeDto.setChecked(true) ;
            }
            ztreeList.add(zTreeDto) ;
        }

        return ztreeList ;

    }


    /**
     * 更新菜单
     * @param params
     * @return
     */
    @Override
    public DataResult<Boolean> updateMenu(MenuParams params){
        Date now = new Date() ;
        Menu menu = new Menu() ;
        BeanUtils.copyProperties(params,menu);
        Long userId = UserUtils.getCurrentUser().getId() ;
        menu.setUpdateUserId(userId);
        menu.setUpdateTime(now);
        menu.setCreateTime(null);
        menu.setCreateUserId(null);
        menu.setDelFlag(null);
        this.generatorIds(menu);
        baseMapper.updateById(menu);
        return DataResult.success() ;
    }
    /**
     * 新增菜单
     * @param params
     * @return
     */
    @Override
    public DataResult<Boolean> insertMenu(MenuParams params){
        Date now = new Date() ;
        Menu menu = new Menu() ;
        BeanUtils.copyProperties(params,menu);
        Long userId = UserUtils.getCurrentUser().getId() ;
        menu.setCreateUserId(userId);
        menu.setUpdateUserId(userId);
        menu.setCreateTime(now);
        menu.setUpdateTime(now);
        menu.setDelFlag(1);
        baseMapper.insertAllColumn(menu);
        this.generatorIds(menu);
        baseMapper.updateAllColumnById(menu);
        return DataResult.success() ;
    }

    private void generatorIds(Menu menu ){
        if( menu.getParentId() == null || menu.getParentId().intValue() == 0 ){
            menu.setParentId(0L);
            menu.setParentIds("0,"+menu.getId());
        }else {
            Menu m = baseMapper.selectById( menu.getParentId() ) ;
            menu.setParentIds( m.getParentIds() + ","+menu.getId());
        }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @Override
    public int deleteCurrentAndChildren(MenuParams params){
        params.setUpdateTime(new Date());
        params.setParentIds(null);
        int num = baseMapper.deleteCurrentAndChildren(params);
        if(params.getDelFlag() == 0 ){
            Menu menu = baseMapper.selectById( params.getId() ) ;
            params.setId(null);
            params.setParentIds( menu.getParentIds() ) ;
            num =+ baseMapper.deleteCurrentAndChildren( params ) ;
        }
        return num ;
    }


}


